<template>
	<view>
		<view class="block">
			<view class="content">
				<view class="orderinfo">
					<view class="row">
						<view class="nominal">订单名称:</view>
						<view class="text">{{orderName}}</view>
					</view>
					<view class="row">
						<view class="nominal">订单金额:</view>
						<view class="text">{{amount}}元</view>
					</view>
				</view>
			</view>
		</view>
		<view class="block">
			<view class="title">
				选择支付方式
			</view>
			<view class="content">
				<view class="pay-list">
					<view class="row" @tap="paytype='yepay'">
						<view class="left">
							<image src="/static/img/wxpay.png"></image>
						</view>
						<view class="center">
							余额支付
						</view>
						<view class="right">
							<radio :checked="paytype=='yepay'" color="#798AD6" />
						</view>
					</view>
					<view class="row" @tap="paytype='fast_pay'">
						<view class="left">
							<image src="/static/img/wxpay.png"></image>
						</view>
						<view class="center">
							快捷支付
						</view>
						<view class="right">
							<radio :checked="paytype=='fast_pay'" color="#798AD6" />
						</view>
					</view>
					<view class="banklist" v-show="paytype == 'fast_pay'">
						<view class="list">
							<view :class="choose_bank == index?'row active':'row '" v-for="(row,index) in addressList"
								:key="index" @tap="select(row,index)">
								<view class="left">
									<view class="head">
										{{row.bank_name|substr(0,2)}}
									</view>
								</view>
								<view class="center">
									<view class="name-tel">
										<view class="name">{{row.bank_name}}</view>
									</view>
									<view class="address">
										{{row.card_id}}
									</view>
								</view>
							</view>
						</view>
						<view class="add" @click="BankAdd">
							<uni-icons type="plus" color="#fff" size="30"></uni-icons><text>新增银行卡</text>
						</view>
					</view>
					<!-- #ifdef APP-PLUS -->
					<view class="row" @tap="paytype='alipay'">
						<view class="left">
							<image src="/static/img/alipay.png"></image>
						</view>
						<view class="center">
							支付宝支付
						</view>
						<view class="right">
							<radio :checked="paytype=='alipay'" color="#798AD6" />
						</view>
					</view>
					<view class="row" @tap="paytype='wxpay'">
						<view class="left">
							<image src="/static/img/share/wx.png"></image>
						</view>
						<view class="center">
							微信支付
						</view>
						<view class="right">
							<radio :checked="paytype=='wxpay'" color="#798AD6" />
						</view>
					</view>
					<!-- #endif -->
					<!-- #ifdef H5 -->
					<view class="row" v-if="!is_weixin" @tap="paytype='alipay_wap'">
						<view class="left">
							<image src="/static/img/alipay.png"></image>
						</view>
						<view class="center">
							支付宝支付
						</view>
						<view class="right">
							<radio :checked="paytype=='alipay_wap'" color="#798AD6" />
						</view>
					</view>
					<view class="row" v-if="!is_weixin" @tap="paytype='wxpay_wap'">
						<view class="left">
							<image src="/static/img/share/wx.png"></image>
						</view>
						<view class="center">
							微信支付
						</view>
						<view class="right">
							<radio :checked="paytype=='wxpay_wap'" color="#798AD6" />
						</view>
					</view>
					<!-- 微信公众号 -->
					<view class="row" v-if="is_weixin" @tap="paytype='wxpay_gz'">
						<view class="left">
							<image src="/static/img/share/wx.png"></image>
						</view>
						<view class="center">
							微信支付
						</view>
						<view class="right">
							<radio :checked="paytype=='wxpay_gz'" color="#798AD6" />
						</view>
					</view>
					<!-- #endif -->
					<!-- #ifdef MP-WEIXIN -->
					<view class="row" @tap="paytype='mppay'">
						<view class="left">
							<image src="/static/img/share/wx.png"></image>
						</view>
						<view class="center">
							微信支付
						</view>
						<view class="right">
							<radio :checked="paytype=='mppay'" color="#798AD6" />
						</view>
					</view>
					<!-- #endif -->

				</view>
			</view>
		</view>
		<view class="pay">
			<view class="btn" @tap="doDeposit">立即支付</view>
			<view class="tis">
				点击立即支付，即代表您同意<view class="terms" @click="pay_agree">
					《条款协议》
				</view>
			</view>
		</view>
		<passwordBox :pShow="pShow" @closeBox="closeBox" @getpassword="getpassword"></passwordBox>
	</view>
</template>

<script>
	import {
		OrderPay
	} from "@/api/api.js"
	import {
		FastList
	} from "@/api/fast.js"
	import passwordBox from "@/components/passwordbox/p_box.vue"
	export default {
		data() {
			return {
				amount: 0,
				orderName: '',
				paytype: 'yepay', //支付类型
				pShow: false,
				password: '',
				available: true,
				is_weixin: true,
				addressList: [{
					bank_name: '建设银行',
					card_id: 12121212121
				}, {
					bank_name: '招商银行',
					card_id: 12121212121
				}],
				choose_bank: -1,
				token_no: '',
				code: ''
			};
		},
		filters: {
			substr(value, start, length) {
				console.log(value);
				if (value == '') return '';
				return value.substring(start, length)
			},
		},
		onLoad(e) {
			this.amount = parseFloat(e.amount).toFixed(2);
			this.orderName = e.order_id
			this.is_wx()

		},
		onShow() {
			this.available = true
			this.BankList()

		},
		components: {
			passwordBox
		},
		methods: {
			select(row, index) {
				console.log(index);
				this.choose_bank = index
				this.token_no = this.addressList[this.choose_bank].token_no
			},
			BankList() {
				FastList().then(res => {
					console.log(res);
					this.addressList = res.data
				})
			},
			BankAdd() {
				uni.navigateTo({
					url: '/pages/user/bindcard/bindbank/index'
				})
			},
			is_wx() {
				// #ifdef H5
				let en = window.navigator.userAgent.toLowerCase()
				// 匹配en中是否含有MicroMessenger字符串
				if (en.match(/MicroMessenger/i) == 'micromessenger') {
					console.log('在微信')
					this.is_weixin = true
				}
				if (en.match(/MicroMessenger/i) != 'micromessenger') {
					console.log('不在微信')
					this.is_weixin = false
				}
				// #endif
			},
			pay_agree() {
				uni.navigateTo({
					url: '/pages/login/pay_agree'
				})
			},
			closeBox() {
				this.pShow = false
			},
			getpassword(e) {
				this.password = e
				this.pShow = false
				this.OrderPay()
			},
			OrderPay() {
				if (this.paytype == 'mppay') {
					var that = this
					uni.login({
						success: (res) => {
							this.code = res.code
							OrderPay({
								order_id: this.orderName,
								pay_type: this.paytype,
								password: this.password,
								token_no: this.token_no,
								code: this.code
							}).then(res => {
								if (res.status == 0) {
									uni.showToast({
										title: res.message,
										icon: "none"
									})
								}
								if (res.status == 1) {
									let paymentData = JSON.parse(res.data);
									uni.requestPayment({
										timeStamp: paymentData.timeStamp,
										nonceStr: paymentData.nonceStr,
										package: paymentData.package,
										signType: 'MD5',
										paySign: paymentData.paySign,
										success: function(result) {
											console.log('success:' + JSON.stringify(
												result));
											setTimeout(() => {
												uni.redirectTo({
													url: '/pages/user/order_list/order_list'
												})
											})
										},
										fail: function(err) {
											console.log('fail:' + JSON.stringify(err));
											that.available = true
										}
									})
								}
							})
						}
					})
				} else {
					OrderPay({
						order_id: this.orderName,
						pay_type: this.paytype,
						password: this.password,
						token_no: this.token_no,
						code: this.code
					}).then(res => {
						if (res.status == 0) {
							uni.showToast({
								title: res.message,
								icon: "none"
							})

						}
						if (res.status == 1) {
							if (this.paytype == 'yepay') {
								uni.showToast({
									title: res.message,
									icon: "none"
								})
								setTimeout(() => {
									uni.redirectTo({
										url: '../../pay/success/success?amount=' + this.amount
									});
								}, 300);
							} else if (this.paytype == 'alipay') {
								//支付宝app支付
								plus.runtime.openURL(res.data.expend.jump_id)
								return false
								uni.requestPayment({
									provider: "alipay",
									orderInfo: res.data,
									success: (data) => {
										this.available = false
										console.log(data);
										uni.showToast({
											title: '支付成功',
											icon: "none"
										})
										setTimeout(() => {
											uni.redirectTo({
												url: '../../pay/success/success?amount=' +
													this.amount
											});
										}, 300)

									},
									fail: (err) => {
										console.log(err);
										this.available = true
									}
								})
							} else if (this.paytype == 'alipay_wap' || this.paytype == 'wxpay_wap') {
								// window.location.href = res.data
								window.location.href = res.data.expend.ali_h5_pay_url
							} else if (this.paytype == 'wxpay_gz') {
								//微信公众号支付
								let appid = res.data
								let tiaozhuan = getApp().globalData.data.websiteUrl +
									'pages/pay/wxgz/gzpay?amount=' +
									this.amount + '&order_id=' + this.orderName
								let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid +
									'&redirect_uri=' + encodeURIComponent(tiaozhuan) +
									'&response_type=code&scope=snsapi_base&state=1#wechat_redirect';
								console.log(url);
								window.location.href = url
							} else if (this.paytype == 'fast_pay') {
								//快捷支付
								uni.navigateTo({
									url: '/pages/pay/payment/sms/index?payment_id=' + res.data.id
								})
								return false
							}
						}
					})
				}

			},
			doDeposit() {
				if (this.paytype == 'yepay') {
					if (this.pShow) {
						return
					}
					this.pShow = true
				} else {
					if (this.available) {
						this.available = false
						this.OrderPay()
					}
				}

				//模板模拟支付，实际应用请调起微信/支付宝
				// uni.showLoading({
				// 	title: '支付中...'
				// });
				// setTimeout(() => {
				// 	uni.hideLoading();
				// 	uni.showToast({
				// 		title: '支付成功'
				// 	});
				// 	setTimeout(() => {
				// 		uni.redirectTo({
				// 			url: '../../pay/success/success?amount=' + this.amount
				// 		});
				// 	}, 300);
				// }, 700)
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #0F172A;
	}

	.banklist {
		color: #fff;

		.add {
			display: flex;
			align-items: center;
			font-size: 14px;
			padding: 10px 2%;
			border-bottom: 1px solid #303C4F;

			text {
				margin-right: 10px;
			}
		}
	}

	.list {
		flex-wrap: wrap;

		.row {
			width: 96vw;
			padding: 20upx 2%;
			color: #fff;

			.left {
				width: 90upx;
				flex-shrink: 0;
				align-items: center;

				.head {
					display: flex;
					width: 70upx;
					height: 70upx;
					background: linear-gradient(to right, #ccc, #aaa);
					color: #fff;
					justify-content: center;
					align-items: center;
					border-radius: 60upx;
					font-size: 25upx;
				}
			}

			.center {
				width: 100%;
				flex-wrap: wrap;

				.name-tel {
					width: 100%;
					align-items: baseline;

					.name {
						font-size: 34upx;
					}

					.tel {
						margin-left: 30upx;
						font-size: 24upx;
						color: #fff;
					}

					.default {

						font-size: 22upx;
						background-color: #798AD6;
						color: #fff;
						padding: 0 18upx;
						border-radius: 24upx;
						margin-left: 20upx;
					}
				}

				.address {
					width: 100%;
					font-size: 24upx;
					align-items: baseline;
					color: #fff;
				}
			}

			.right {
				flex-shrink: 0;
				align-items: center;
				margin-left: 20upx;

				.icon {
					justify-content: center;
					align-items: center;
					width: 80upx;
					height: 60upx;
					border-left: solid 1upx #303C4F;
					font-size: 40upx;
					color: #fff;
				}
			}
		}

		.active {
			background: #d1d1d1;
			border-radius: 4px;
		}
	}

	.block {
		width: 94%;
		padding: 0 3% 40upx 3%;

		.title {
			width: 100%;
			font-size: 34upx;
			color: #fff;
			font-weight: bold;
			margin-bottom: 10px;
		}

		.content {
			.orderinfo {
				width: 100%;
				border-bottom: solid 1upx #303C4F;

				.row {
					width: 100%;
					height: 90upx;
					display: flex;
					align-items: center;
					color: #fff;

					.nominal {
						flex-shrink: 0;
						font-size: 14px;
					}

					.text {
						width: 70%;
						margin-left: 10upx;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
						font-size: 14px;
					}
				}
			}

			.pay-list {
				width: 100%;
				border-bottom: solid 1upx #303C4F;

				.row {
					width: 100%;
					height: 120upx;
					display: flex;
					align-items: center;
					box-sizing: border-box;
					color: #fff;

					.left {
						width: 100upx;
						flex-shrink: 0;
						display: flex;
						align-items: center;

						image {
							width: 70upx;
							height: 70upx;
							border-radius: 50%;
						}
					}

					.center {
						width: 100%;
						font-size: 30upx;
					}

					.right {
						width: 100upx;
						flex-shrink: 0;
						display: flex;
						justify-content: flex-end;
					}
				}
			}
		}
	}

	.pay {
		margin-top: 20upx;
		width: 100%;
		display: flex;
		justify-content: center;
		flex-wrap: wrap;

		.btn {
			width: 70%;
			height: 80upx;
			border-radius: 80upx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			background-color: #798AD6;
			box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.2);
		}

		.tis {
			margin-top: 10upx;
			width: 100%;
			font-size: 24upx;
			display: flex;
			justify-content: center;
			align-items: baseline;
			color: #999;

			.terms {
				color: #5a9ef7;
			}
		}
	}
</style>